package com.wanxi.servlet.user;

import com.alibaba.fastjson2.JSONObject;
import com.wanxi.dto.ResultDTO;
import com.wanxi.model.ResultModel;
import com.wanxi.service.impl.UserServiceImpl;
import com.wanxi.model.UserModel;
import com.wanxi.util.MD5;
import com.wanxi.util.Tool;
import com.wanxi.vo.UserVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/back/user/add")
public class UserAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        1.乱码处理
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        2.得到前端的指
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String sex = req.getParameter("sex");
        String phone = req.getParameter("phone");
        String email = req.getParameter("email");
        String address = req.getParameter("address");
        String birthday = req.getParameter("birthday");
        String hobby = req.getParameter("hobby");
        String remark = req.getParameter("remark");
        String enable = req.getParameter("enable");
//        3.封装model
        UserVO userVO = UserVO.builder()
                .username(username)
                .password(MD5.encoderByMd5(password))
                .sex(sex)
                .phone(phone)
                .email(email)
                .address(address)
                .birthday(Tool.strToDate(birthday))
                .hobby(hobby)
                .remark(remark)
                .enable(Tool.strToInt(enable))
                .build();
//        4.调用逻辑层处理逻辑
        UserServiceImpl userService = new UserServiceImpl();
        ResultDTO resultDTO = userService.add(userVO);
//        5.将数据转发给前端：io流的方式
//        以后前后端数据格式统一用json对象
//        {“key”:"value",“key”:"value"}
//        {“username”:"aaa",“password”:"bbbbb","sex":"男"}
//        所以需要将int转换为json的数据类型格式
//        需要引入第三方jar包：fastJson
        resp.getWriter().println(JSONObject.toJSONString(resultDTO));
//        req.getRequestDispatcher("/aaa.jsp").forward(req,resp);
    }
}
